<!--
 * @description 组件
 * @author: 陈陈
 * @date: 2025/05/09
 * @phone: 18560000860
 * @email: 18560000860@qq.com
 * @company: 济南晨霜信息技术有限公司 /
 * @business: 承接前后端项目的开发
-->
<template>
	<view
		class="pure-date-picker"
		:class="_classes"
		:style="_styles"
	>
		<pure-picker
			v-bind="{
				..._props,
				dataMode: 'children',
				cols: cols,
				list: _list,
				indexes: undefined,
				values: _values
			}"
			@onConfirm="_handleConfirm"
			@onCancel="_handleCancel"
			@onClose="_handleClose"
			@onNot="_handleNot"
			@onOk="_handleOk"
			@onChange="_handleChange"
		></pure-picker>
	</view>
</template>

<script>
	import commonComponentOptions from "../../config/options";
	export default {
		options: commonComponentOptions
	};
</script>

<script setup>
	import { computed } from "vue";
	import props from "./props";
	import { isString, isNumber, isDate, isFunction, dateParse, dateData } from "@/uni_modules/pure-utils";

	// 数据列表
	const _list = computed(() => {
		// 年份列表
		let years = _getYears();
		// 添加月份
		years = _addMonth(years);
		// 添加日期
		years = _addDays(years);
		return years;
	});

	// 值
	const _values = computed(() => {
		const date = dateData(_props?.date?.replace(/年|月|日/g, "-")?.replace(/时|分|秒/g, ":"));
		return [date.year, date.month, date.day];
	});

	// -------------------------------------------------------------------------------- Props
	const _props = defineProps(props);

	// -------------------------------------------------------------------------------- Emits
	const _emits = defineEmits(["onClose", "onConfirm", "onCancel", "onNot", "onOk", "onChange"]);

	// -------------------------------------------------------------------------------- Classes
	const _classes = computed(() => {
		const array = [];
		return array;
	});

	// -------------------------------------------------------------------------------- Styles
	const _styles = computed(() => {
		const array = [];
		return array;
	});

	// 获取年份
	function _getYears() {
		if (!_props?.startDate || !_props?.endDate) return [];
		const startDate = dateParse(_props?.startDate);
		const endDate = dateParse(_props?.endDate);

		const years = [];
		for (let i = startDate.getFullYear(); i <= endDate.getFullYear(); i++) {
			years.push({
				label: isFunction(_props?.formatter) ? _props?.formatter(i, "year") : `${i}年`,
				value: i,
				full: i.toString()
			});
		}
		return years;
	}

	// 添加月份
	function _addMonth(years) {
		years.forEach((year) => {
			year.children = _getMonths();
		});
		return years;
	}

	// 获取月份
	function _getMonths() {
		const months = [];
		for (let i = 1; i <= 12; i++) {
			months.push({
				label: isFunction(_props?.formatter) ? _props?.formatter(i, "month") : `${i}月`,
				value: i,
				full: (i < 10 ? `0${i}` : i).toString()
			});
		}
		return months;
	}

	// 添加日期
	function _addDays(years) {
		years.forEach((year) => {
			const months = year?.children || [];
			months.forEach((month) => {
				month.children = _getDays(year.value, month.value);
			});
		});
		return years;
	}

	// 获取日期
	function _getDays(year, month) {
		const days = [];
		if (!year || !month) return days;

		// 获取这个月有多少天
		const daysInMonth = new Date(year, month, 0).getDate();
		for (let i = 1; i <= daysInMonth; i++) {
			days.push({
				label: isFunction(_props?.formatter) ? _props?.formatter(i, "day") : `${i}日`,
				value: i,
				full: (i < 10 ? `0${i}` : i).toString()
			});
		}
		return days;
	}

	// 确认按钮点击事件
	function _handleConfirm(indexes, items) {
		const str = items.map((item) => item.full).join("-");
		_emits("onConfirm", str, items);
	}

	// 取消按钮点击事件
	function _handleCancel() {
		_emits("onCancel");
	}

	// 关闭按钮点击事件
	function _handleClose() {
		_emits("onClose");
	}

	// Header 右侧按钮点击事件
	function _handleOk(indexes, items) {
		const str = items.map((item) => item.full).join("-");
		_emits("onOk", str, items);
	}

	// Header 左侧按钮点击事件
	function _handleNot() {
		_emits("onNot");
	}

	// 选择器改变事件
	function _handleChange(indexes, items) {
		const str = items.map((item) => item.full).join("-");
		_emits("onChange", str, items);
	}
</script>

<style scoped lang="scss">
	@import "./style.scss";
</style>
